package org.example.video.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.commons.entity.Comment;
import com.example.commons.entity.Video;

import jakarta.annotation.Resource;
import org.example.video.mapper.VideoMapper;
import org.example.video.service.CommentService;
import org.example.video.service.VideoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: authority-v3.0.7
 * @ClassName:VideoServiceImpl
 * @description: 视频列表 service实现类
 * @author:dyy
 * @Version 3.0
 **/

@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {
    @Resource
    private CommentService commentService;

    @Resource
    private VideoMapper videoMapper;

    @Override
    @Transactional
    public boolean removeVideoById(Long id) {
        // 删除视频
        boolean removed = this.removeById(id);
        System.out.println(removed);
        if (removed) {
            // 删除关联评论
            LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Comment::getVid, id);
            System.out.println(id);
            commentService.remove(wrapper);
        }
        return removed;
    }

    @Override
    @Transactional
    public boolean removeVideoByIds(List<Long> ids) {
        // 删除视频
        boolean removed = this.removeByIds(ids);
        if (removed) {
            // 删除关联评论
            LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(Comment::getVid, ids);
            commentService.remove(wrapper);
        }
        return removed;
    }

    @Override
    public void incrementClick(Long id) {
        videoMapper.incrementClick(id);
    }
}
